<template>
    <el-dialog
        model-value="dialogVisible"
        :title="dialogTitle"
        width="30%"
        @close="handleClose"
    >

        <el-form
            ref="formRef"
            :model="form"
            :rules="rules"
            label-width="100px"
        >
            <el-form-item label="角色名" prop="name">
                <el-input v-model="form.name" />
            </el-form-item>

            <el-form-item label="权限字符" prop="code">
                <el-input v-model="form.code" />
            </el-form-item>


            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" :rows="4"/>
            </el-form-item>


        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button  @click="handleClose">取消</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script setup>
import {defineEmits, defineProps, ref, watch} from "vue";
import requestUtil,{getServerUrl} from "../../../../utils/request.js";
import { ElMessage } from 'element-plus'


const tableData=ref([])


const props=defineProps(
    {
        id:{
            type:Number,
            default:-1,
            required:true
        },
        dialogTitle:{
            type:String,
            default:'',
            required:true
        },
        dialogVisible:{
            type:Boolean,
            default:false,
            required:true
        }
    }
)


const form=ref({
    id:-1,
    name:"",
    code:"",
    remark:""
})


const rules=ref({
    name:[
        { required: true, message: '请输入角色名'},
    ],
    code: [{ required: true, message: "权限字符不能为空", trigger: "blur" }, { type: "code", message: "请输入正确的权限字符", trigger: ["blur", "change"] }],
})

const formRef=ref(null)

const initFormData=async(id)=>{
    const res=await requestUtil.get("sys/role/"+id);
    form.value=res.data.data;
}

watch(
    ()=>props.dialogVisible,
    ()=>{
        let id=props.id;
        if(id!=-1){
            initFormData(id)
        }else{

            form.value={
                id:-1,
                name:"",
                code:"",
                remark:""
            }

        }
    }
)

/*创建一个事件名为update:modelValue的initRoleList事件*/
const emits=defineEmits(['update:modelValue','initRoleList'])

const handleClose=()=>{
    emits('update:modelValue',false)
}


const handleConfirm=()=>{
    formRef.value.validate(async(valid)=>{
        if(valid){
            let result;
            if(props.id===-1)
                result=await requestUtil.post("sys/role/add",form.value);
            else
                result=await requestUtil.post("sys/role/save",form.value)
            let data=result.data;
            if(data.status==200){
                ElMessage.success(data.data)
                formRef.value.resetFields();
                emits("initRoleList")
                handleClose();
            }else{
                ElMessage.error(data.data);
            }
        }else{
            console.log("fail")
        }
    })
}


</script>

<style lang="scss" scoped>

</style>